package com.atguigu.c_designed.m2stucturalModel.s7proxy;

import java.lang.reflect.InvocationHandler;
import java.lang.reflect.Method;
import java.lang.reflect.Proxy;

public class JdkProxyClient {
    public static void main(String[] args) {
        JdkProxy<TeacherDao> proxy = new JdkProxy(new TeacherDaoImpl());
        TeacherDao proxyInstance = proxy.getProxyInstance();
        String teach2 = proxyInstance.Teach2("张");
        System.out.println(teach2);
    }
}

class JdkProxy<T> {

    private Object target;

    public JdkProxy(Object target) {
        this.target = target;
    }

    public T getProxyInstance() {
        return (T) Proxy.newProxyInstance(target.getClass().getClassLoader(), target.getClass().getInterfaces(), new InvocationHandler() {
            @Override
            public Object invoke(Object proxy, Method method, Object[] args) throws Throwable {
                System.out.println("jdk动态代理开始");
                Object invoke = method.invoke(target, args);
                System.out.println("jdk动态代理结束。。。");
                return invoke;
            }
        });
    }
}